package com.fastadmin.mapper.impl;

import com.fastadmin.pojo.UserRole;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.builder.SQL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static org.asynchttpclient.util.StringBuilderPool.DEFAULT;

@Repository
public class UserRoleMapper {

    @Autowired
    EasyJdbcTemplate easyJdbcTemplate;

    public List<UserRole> findByUserId(Object userId) {
        StringBuilder sql = DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	* ");
        sql.append(" FROM ");
        sql.append(" 	t_user_role ");
        sql.append(" WHERE ");
        sql.append(" 	user_id = ? ");
        List<UserRole> userRoles = easyJdbcTemplate.find(sql.toString(), UserRole.class, userId);
        return userRoles;
    }

    public int deleteByUserId(Long user_id) {
        return easyJdbcTemplate.delete(SQL.BUILD().DELETE_FROM("t_user_role").WHERE("user_id=?"), user_id);
    }

    public int batchSave(ArrayList<UserRole> userRoles) {
        return this.easyJdbcTemplate.batchInsert(userRoles);
    }
}
